package cn.cdutetc.library.controller;

import cn.cdutetc.library.common.ApiResponse;
import cn.cdutetc.library.entity.dto.PaymentDTO;
import cn.cdutetc.library.entity.dto.PaymentStatisticsVO;
import cn.cdutetc.library.entity.dto.PaymentVO;
import cn.cdutetc.library.service.PaymentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 支付管理控制器
 */
@RestController
@RequestMapping("/api/payments")
@Tag(name = "支付管理", description = "支付管理相关接口")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping
    @Operation(summary = "创建支付记录", description = "创建新的支付记录")
    public ApiResponse<Integer> createPayment(@RequestBody @Validated PaymentDTO paymentDTO) {
        Integer paymentId = paymentService.createPayment(paymentDTO);
        return ApiResponse.success(paymentId);
    }

    @PostMapping("/{paymentId}/complete")
    @Operation(summary = "完成支付", description = "完成支付操作")
    public ApiResponse<Boolean> completePayment(
            @PathVariable @Parameter(description = "支付ID") Integer paymentId,
            @RequestParam @Parameter(description = "支付方式(alipay/wechat/cash)") String payMethod) {
        boolean result = paymentService.completePayment(paymentId, payMethod);
        return ApiResponse.success(result);
    }

    @PostMapping("/{paymentId}/cancel")
    @Operation(summary = "取消支付", description = "取消支付操作")
    public ApiResponse<Boolean> cancelPayment(@PathVariable @Parameter(description = "支付ID") Integer paymentId) {
        boolean result = paymentService.cancelPayment(paymentId);
        return ApiResponse.success(result);
    }

    @GetMapping("/{paymentId}")
    @Operation(summary = "获取支付详情", description = "根据ID获取支付详情")
    public ApiResponse<PaymentVO> getPaymentById(@PathVariable @Parameter(description = "支付ID") Integer paymentId) {
        PaymentVO paymentVO = paymentService.getPaymentById(paymentId);
        return ApiResponse.success(paymentVO);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询支付记录", description = "分页查询支付记录")
    public ApiResponse<IPage<PaymentVO>> pagePayments(
            @RequestParam(defaultValue = "1") @Parameter(description = "页码") Integer page,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页数量") Integer size,
            @RequestParam(required = false) @Parameter(description = "搜索关键词") String keyword,
            @RequestParam(required = false) @Parameter(description = "费用类型(overdue_fee/damage_fee/loss_fee)") String feeType,
            @RequestParam(required = false) @Parameter(description = "支付状态(paid/unpaid/refunded)") String status) {
        IPage<PaymentVO> result = paymentService.pagePayments(page, size, keyword, feeType, status);
        return ApiResponse.success(result);
    }

    @GetMapping("/user/{userId}/total")
    @Operation(summary = "获取用户支付总额", description = "获取用户支付总额")
    public ApiResponse<Double> getUserPaymentTotal(@PathVariable @Parameter(description = "用户ID") Integer userId) {
        Double total = paymentService.getUserPaymentTotal(userId);
        return ApiResponse.success(total);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户支付记录", description = "分页获取指定用户的支付记录")
    public ApiResponse<IPage<PaymentVO>> getUserPayments(
            @PathVariable @Parameter(description = "用户ID") Integer userId,
            @RequestParam(defaultValue = "1") @Parameter(description = "页码") Integer page,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页数量") Integer size,
            @RequestParam(required = false) @Parameter(description = "费用类型(overdue_fee/damage_fee/loss_fee)") String feeType,
            @RequestParam(required = false) @Parameter(description = "支付状态(paid/unpaid/refunded)") String status) {
        IPage<PaymentVO> result = paymentService.getUserPayments(userId, page, size, feeType, status);
        return ApiResponse.success(result);
    }

    @GetMapping("/user/{userId}/stats")
    @Operation(summary = "获取用户支付统计", description = "获取指定用户的支付统计数据")
    public ApiResponse<PaymentStatisticsVO> getUserPaymentStats(@PathVariable @Parameter(description = "用户ID") Integer userId) {
        PaymentStatisticsVO statistics = paymentService.getUserPaymentStatistics(userId);
        return ApiResponse.success(statistics);
    }

    @PostMapping("/borrow/{recordId}/fine")
    @Operation(summary = "处理借阅罚款支付", description = "处理借阅罚款支付")
    public ApiResponse<Boolean> handleBorrowFinePayment(
            @PathVariable @Parameter(description = "借阅记录ID") Integer recordId,
            @RequestParam @Parameter(description = "支付方式(alipay/wechat/cash)") String payMethod) {
        boolean result = paymentService.handleBorrowFinePayment(recordId, payMethod);
        return ApiResponse.success(result);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获取支付统计数据", description = "获取支付相关的统计数据")
    public ApiResponse<PaymentStatisticsVO> getPaymentStatistics() {
        PaymentStatisticsVO statistics = paymentService.getPaymentStatistics();
        return ApiResponse.success(statistics);
    }
}
